﻿#include "HelloWorldScene.h"
#include "communicate/BrickClient.h"
#include "ReadChineseFromXML.h"
#include "SimpleAudioEngine.h"
#include "SelectGateScene.h"
#include "ConnectLayer.h"

#include <unistd.h> 
#include <iostream>

USING_NS_CC;
using namespace std;

Scene* HelloWorld::createScene()
{
	// 'scene' is an autorelease object
	auto scene = Scene::create();

	//场景创建的时候，默认有camera子节点
	log("special size=%u", scene->getChildren().size());
	// 'layer' is an autorelease object
	auto layer = HelloWorld::create();

	// add layer as a child to scene
	scene->addChild(layer);
	layer->setTag(1);

	return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	//////////////////////////////
	//////////////////////////////
	// 1. super init first
	if (!Layer::init())
	{
		return false;
	}

	auto visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	//设置背景图片
	auto background = Sprite::create("star29.jpg");
	background->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
	//根据缩放设置实际大小
	Size size_back = background->getContentSize();  //获取精灵大小(设置图片的时候，精灵大小为图片大小)
													//根据实际宽度和总宽度，计算期望缩放比率
	float scaleX = (float)visibleSize.width / (float)size_back.width;
	float scaleY = (float)visibleSize.height / (float)size_back.height;
	//background->setScale(scaleX, scaleY);    //缩放

	this->addChild(background);

	//通过xml文件中的key获取value
	auto startitem = MenuItemImage::create("begin_btn.png", "begin_btn.png", CC_CALLBACK_1(HelloWorld::CreateGate, this));

	auto selectitem = MenuItemImage::create("square.png", "square.png", CC_CALLBACK_1(HelloWorld::ChooseGate, this));

	auto menu = Menu::create(startitem, selectitem, nullptr);
	menu->alignItemsVertically();

	menu->setPosition(visibleSize.width / 2, visibleSize.height / 2 + 40);
	menu->setTag(tag_menu);

	addChild(menu);

	/////////////////////////////
	// 3. add your codes below...
	this->scheduleOnce(schedule_selector(HelloWorld::ConnectToServer), 0.1f); //在1.0f之后执行，并且只执行一次。  

	return true;
}

void HelloWorld::ConnectToServer(float df)
{
	BrickClient::GetInstance()->ProcessClose();
}

void HelloWorld::DisableMenu()
{
	Menu * menu = (Menu *)this->getChildByTag(tag_menu);

	menu->setEnabled(false);
	menu->setColor(Color3B(100, 100, 100));
}

void HelloWorld::ActiviMenu()
{
	Menu * menu = (Menu *)this->getChildByTag(tag_menu);

	menu->setEnabled(true);
	menu->setColor(Color3B(255, 255, 255));
}

/*
void HelloWorld::AddGray(cocos2d::Sprite *sp)
{
	do
	{
		GLchar* pszFragSource =	"#ifdef GL_ES \n  \
			precision mediump float;  \n\
			#endif   \n\
			uniform sampler2D u_texture;  \n \
			varying vec2 v_texCoord; \n  \
			varying vec4 v_fragmentColor;  \n \
			void main(void)  \
		{ \n \
			// Convert to greyscale using NTSC weightings \n \
			vec4 col = texture2D(u_texture, v_texCoord); \n \
			float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n \
			gl_FragColor = vec4(grey, grey, grey, col.a);  \
		}";  

		CCGLProgram* pProgram = new CCGLProgram();
		pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);

		sp->setShaderProgram(pProgram);
		CHECK_GL_ERROR_DEBUG();

		sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
		sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
		sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
		CHECK_GL_ERROR_DEBUG();

		sp->getShaderProgram()->link();
		CHECK_GL_ERROR_DEBUG();

		sp->getShaderProgram()->updateUniforms();
		CHECK_GL_ERROR_DEBUG();
	} while (0);
}

void HelloWorld::RemoveGray(cocos2d::Sprite *sp)
{
	do
	{
		CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor);
		sp->setShaderProgram(pProgram);
		CHECK_GL_ERROR_DEBUG();

		sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
		sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
		sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
		CHECK_GL_ERROR_DEBUG();

		sp->getShaderProgram()->link();
		CHECK_GL_ERROR_DEBUG();

		sp->getShaderProgram()->updateUniforms();
		CHECK_GL_ERROR_DEBUG();
	} while (0);
}
*/

void HelloWorld::CreateGate(Object * pSender)
{
	//默认第一关
	unsigned id = 1;

	SelectGateScene::CreateCustomGate(id);
}

void HelloWorld::ChooseGate(cocos2d::Object * pSender)
{
	Director::getInstance()->pushScene(SelectGateScene::createScene());
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
	Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}
